#include "stdafx.h"


	namespace network
	{
		namespace helper
		{

	MulticastSenderHelper::MulticastSenderHelper( const boost::asio::ip::address& multicast_address): 
		m_ioService(),
		m_endpoint(multicast_address, multicast_port),
		m_socket(m_ioService, m_endpoint.protocol()),
		m_serverEndpoint(),
		m_timer(m_ioService),
		m_messageCount(0),
		m_data(new std::vector<unsigned char>(max_length))
	{
		boost::asio::ip::multicast::enable_loopback lb(true);
		boost::asio::ip::multicast::hops ttl(64);
		m_socket.set_option(ttl);
		m_socket.set_option(lb);

	}

	MulticastSenderHelper::~MulticastSenderHelper()
	{
	}

	void MulticastSenderHelper::handle_receive_from(const boost::system::error_code& error, size_t bytes_recvd)
	{
		if (!error)
		{
			m_socket.async_receive_from( boost::asio::buffer(*m_data, max_length), m_serverEndpoint,
						boost::bind(&MulticastSenderHelper::handle_receive_from, this,
						boost::asio::placeholders::error,
						boost::asio::placeholders::bytes_transferred));
		}
	}

	
	void MulticastSenderHelper::handle_timeout()
	{
#if defined(_DEBUG) && defined(_OUTPUT)
		std::cout << "Timeout reach, requested operation abort" << std::endl;
#endif
		m_ioService.stop();
	}
		
	void MulticastSenderHelper::close()
	{
		m_ioService.post( boost::bind(&MulticastSenderHelper::handle_stop, this) );
		m_socket.close();
	}

	void MulticastSenderHelper::handle_stop()
	{
#if defined(_DEBUG) && defined(_OUTPUT)
		std::cout << " [ MulticastSenderHelper ]" << std::endl;
		std::cout << " stop I/O services on multicast asker " << std::endl;
#endif
		
	}

	}	//	namespace helper
}	//	//	namespace network